package com.cq.smartelebicycle.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

public class WifiUtils {
	
	private WifiManager localWifiManager;
	// private List<ScanResult> wifiScanList;/
	private List<WifiConfiguration> wifiConfigList;
//	private WifiInfo wifiConnectedInfo;
//	private WifiLock wifiLock;
	
	public static final int WAIT_WIFI = 1;
	public static final int UI_NEED_UPDATE = 2;
	public static final int UI_NONEED_UPDATE = 3;

	public WifiUtils(Context context) {
		localWifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
	}

	public int WifiCheckState() {

		return localWifiManager.getWifiState();
	}

	public void WifiOpen() {
		if (!localWifiManager.isWifiEnabled()) {
			localWifiManager.setWifiEnabled(true);
		}
	}

	public void WifiClose() {
		if (!localWifiManager.isWifiEnabled()) {
			localWifiManager.setWifiEnabled(false);
		}
	}

	public void WifiStartScan() {
		localWifiManager.startScan();
	}
	
	public List<ScanResult> getScanResults() {
		List<ScanResult> list = localWifiManager.getScanResults();
		if (list != null && list.size() > 0) {
			sortByLevel(list);
		}
		return list;
	}
	
	public int scanWifi(List<ScanResult> wifiListString) {
		WifiOpen();
		WifiStartScan();
		// 0正在关闭,1WIFi不可用,2正在打开,3可用,4状态不可zhi
		while (WifiCheckState() != WifiManager.WIFI_STATE_ENABLED) {
			// 等待Wifi开启
			return WAIT_WIFI;
			
		}
		try {
			Thread.sleep(3000);// 休眠3s，不休眠则会在程序首次开启WIFI时候，处理getScanResults结果，wifiResultList.size()发生异常
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<ScanResult> list = getScanResults();
		getConfiguration();
		if (scanResultToString(list, wifiListString)) {
			return UI_NEED_UPDATE;
		} else {
			return UI_NONEED_UPDATE;
			//Log.i("scanResultToSting", "fail");
		}
		
	}

	// 将搜索到的wifi根据信号强度从强到弱进行排序
	public void sortByLevel(List<ScanResult> list) {
		for (int i = 0; i < list.size(); i++)
			for (int j = 1; j < list.size(); j++) {
				if (list.get(i).level < list.get(j).level) // level属性即为强度
				{
					ScanResult temp = null;
					temp = list.get(i);
					list.set(i, list.get(j));
					list.set(j, temp);
				}
			}
	}
	public boolean scanResultToString(List<ScanResult> listScan, List<ScanResult> listStr) {
		listStr.clear();
		boolean needupdate = false;
		for (int i = 0; i < listScan.size(); i++) {
			ScanResult strScan = listScan.get(i);
			// String str = strScan.SSID+"--"+strScan.BSSID;
			ScanResult str = strScan;
			if (listStr.add(str)) {
				needupdate = true;
			}
		}
		return needupdate;
	}

	public void getConfiguration() {
		wifiConfigList = localWifiManager.getConfiguredNetworks();
		for (int i = 0; i < wifiConfigList.size(); i++) {
			Log.i("getConfiguration", wifiConfigList.get(i).SSID);
			Log.i("getConfiguration",
					String.valueOf(wifiConfigList.get(i).networkId));
		}
	}

	public int IsConfiguration(String SSID) {
		Log.i("IsConfiguration", String.valueOf(wifiConfigList.size()));
		for (int i = 0; i < wifiConfigList.size(); i++) {
			Log.i(wifiConfigList.get(i).SSID,
					String.valueOf(wifiConfigList.get(i).networkId));
			if (wifiConfigList.get(i).SSID.equals(SSID)) {// ��ַ��ͬ
				return wifiConfigList.get(i).networkId;
			}
		}
		return -1;
	}

	public int AddWifiConfig(List<ScanResult> wifiList, String ssid, String pwd) {
		int wifiId = -1;
		for (int i = 0; i < wifiList.size(); i++) {
			ScanResult wifi = wifiList.get(i);
			if (wifi.SSID.equals(ssid)) {
				Log.i("AddWifiConfig", "equals");
				WifiConfiguration wifiCong = new WifiConfiguration();
				wifiCong.SSID = "\"" + wifi.SSID + "\"";
				wifiCong.preSharedKey = "\"" + pwd + "\"";
				wifiCong.hiddenSSID = false;
				wifiCong.status = WifiConfiguration.Status.ENABLED;
				wifiId = localWifiManager.addNetwork(wifiCong);
				if (wifiId != -1) {
					return wifiId;
				}
			}
		}
		return wifiId;
	}

	public boolean ConnectWifi(int wifiId) {
		for (int i = 0; i < wifiConfigList.size(); i++) {
			WifiConfiguration wifi = wifiConfigList.get(i);
			if (wifi.networkId == wifiId) {
				while (!(localWifiManager.enableNetwork(wifiId, true))) {
					Log.i("ConnectWifi",
							String.valueOf(wifiConfigList.get(wifiId).status));
				}
				return true;
			}
		}
		return false;
	}
/*
	public void createWifiLock(String lockName) {
		wifiLock = localWifiManager.createWifiLock(lockName);
	}

	public void acquireWifiLock() {
		wifiLock.acquire();
	}

	public void releaseWifiLock() {
		if (wifiLock.isHeld()) {// �ж��Ƿ���
			wifiLock.release();
		}
	}

	public void getConnectedInfo() {
		wifiConnectedInfo = localWifiManager.getConnectionInfo();
	}

	public String getConnectedMacAddr() {
		return (wifiConnectedInfo == null) ? "NULL" : wifiConnectedInfo
				.getMacAddress();
	}

	public String getConnectedSSID() {
		return (wifiConnectedInfo == null) ? "NULL" : wifiConnectedInfo
				.getSSID();
	}

	public int getConnectedIPAddr() {
		return (wifiConnectedInfo == null) ? 0 : wifiConnectedInfo
				.getIpAddress();
	}

	public int getConnectedID() {
		return (wifiConnectedInfo == null) ? 0 : wifiConnectedInfo
				.getNetworkId();
	}
	*/
}
